import pandas as pd


def cal_value_heap(l, index=1):
    size = l.split()
    return int(size[index])


class MeminfoFrameParser(object):
    @staticmethod
    def parse_files(paths):
        sers = []
        for path in paths:
            print(path)
            with open(path, 'r') as f:
                lines = f.readlines()
                ser = MeminfoFrameParser.parse_frame(lines)
                sers.append(ser)
        return sers

    @staticmethod
    def parse_frame(lines):
        dic = {}
        point = 8
        time = None

        for l in lines:
            if point == 0:
                break
            elif "Java Heap:" in l:
                dic['Java Heap'] = cal_value_heap(l, 2)
                point -= 1
            elif "Native Heap:" in l:
                dic['Native Heap'] = cal_value_heap(l, 2)
                point -= 1
            elif "TOTAL:" in l:
                dic['TOTAL'] = cal_value_heap(l)
                print(dic['TOTAL'])
                point -= 1
            elif "Code:" in l:
                dic['Code'] = cal_value_heap(l)
                point -= 1
            elif "Stack:" in l:
                dic['Stack'] = cal_value_heap(l)
                point -= 1
            elif "Graphics:" in l:
                dic['Graphics'] = cal_value_heap(l)
                point -= 1
            elif "Other:" in l:
                dic['Other'] = cal_value_heap(l, 2)
                point -= 1
            elif "System:" in l:
                dic['System'] = cal_value_heap(l)
                point -= 1
        ser = pd.Series(dic, name=time)
        return ser
